<template>
	<view>
		<view class="feedback-title">意见反馈：</view>
		<view class="feedback-content">
			<textarea class="feedback-textarea" v-model="content" placeholder="请输入你要反馈的意见" />
		</view>
		<view class="feedback-title">反馈图片:</view>
		<view class="feedback-image-box">
			<view class="feedback-image-item" v-for="(item,index) in imageList" :key="index">
				<view class="close-icon" @click="del(index)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="item.url" mode=""></image>
				</view>
			</view>
			<view class="feedback-image-item" @click="addImage" v-if="imageList.length < 5">
				<view class="image-box">
					<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
				</view>
			</view>
		</view>
		<button class="button" type="primary" @click="submit">提交反馈意见</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageList: [],
				content: ''
			};
		},
		methods: {
			del(index) {
				this.imageList.splice(index,1)
			},
			addImage() {
				const count = 5 - this.imageList.length
				uni.chooseImage({
					count: count,
					success: (res) => {
						console.log(res);
						const tempfilepath = res.tempFilePaths
						tempfilepath.forEach((item,index) => {
							// 处理 h5 的图片选择数
							if(index < count) {
								this.imageList.push({
									url: item
								})
							}
						})
					}
				})
			},
			async submit() {
				let imagePath = []
				uni.showLoading()
				for(let i = 0; i < this.imageList.length; i++) {
					const filePath = this.imageList[i].url
					filePath = await this.uploadFiles(filePath)
					imagePath.push(filePath)
				}
				
				this.updateFeedback({
					content: this.content,
					feedbackImages: imagePath
				})
			},
			async uploadFiles(filePath) {
				const res = await uniCloud.uploadFile({
					cloudPath: filePath
				})
				return res.fileID
			},
			updateFeedback({content,feedbackImages}) {
				this.$api.update_feedback({
					content,
					feedbackImages,
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: '反馈提交成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/tabbar/my/my'
						})
					}, 1500)
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: '反馈提交失败',
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.feedback-title {
		margin: 15px;
		margin-bottom: 0;
		font-size: 14px;
		color: #666;
	}
	.feedback-content {
		margin: 15px;
		padding: 10px;
		box-sizing: border-box;
		border: 1px #eee solid;
		border-radius: 5px;
		.feedback-textarea {
			font-size: 12px;
			width: 100%;
			height: 100px;
		}
	}
	.feedback-image-box {
		display: flex;
		flex-wrap: wrap;
		padding: 10px;
		position: relative;
		.feedback-image-item {
			position: relative;
			width: 33.33%;
			height: 0;
			padding-top: 33.33%;
			box-sizing: border-box;
			.close-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 22px;
				height: 22px;
				background-color: #ff5a5f;
				border-radius: 50%;
				overflow: hidden;
				position: absolute;
				top: 0px;
				right: 0px;
				z-index: 2;
			}
			.image-box {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 5px;
				right: 5px;
				bottom: 5px;
				left: 5px;
				border: 1px #f5f5f5 solid;
				border-radius: 5px;
				overflow: hidden;
				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	.button {
		margin: 0 15px;
		background-color: $mk-basic-color;
	}
</style>
